public class LifeGame {
    private GameMap gameMap;

    public void init(GameMap gameMap) {
        this.gameMap = gameMap;
    }

    public void gameCycle() {
        int rows = gameMap.getRows();
        int cols = gameMap.getCols();

        // 下一状态表
        int[][] nextMap = new int[rows][cols];

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++) {
                // 获取当前细胞[i, j]周围的活细胞数
                int neighbor_count = gameMap.getNeighborCount(i, j);
                int state;

                // 判断下一状态
                if (neighbor_count == 3)
                    state = 1;
                else if (neighbor_count == 2)
                    state = gameMap.get(i, j);
                else
                    state = 0;

                // 为细胞[i, j]的下一状态赋值
                nextMap[i][j] = state;
            }
        gameMap.set(nextMap);
    }
}
